
#include "apdu_lib_app_mgr.h"

#include "apdu_lib_common.h"

#include "../apdu.core/apdu.h"

#include "../../mk_lib/mk_auto_ptr.h"
#include "../../mk_lib/mk_utility.h"

#include <stdlib.h>
#include <string.h>
#include "../apdu.algo/mac.h"

extern int	g_sw;


int app_create_application( HANDLE devHandle, unsigned char * app_info, int info_size )
{
	apdu * newApdu  =  NULL;
	newApdu = get_apdu_mgr()->create_apdu_create_application(app_info, info_size);

	mk_auto_ptr<apdu> ap(newApdu);

	if( 0 != get_dev_mgr()->transmit_apdu(devHandle, newApdu, &g_sw))
	{
		return 1;
	}

	if(g_sw != 0x9000)
	{
		return 2;
	}

	return 0;
}


int app_update_application( HANDLE devHandle, int app_id, unsigned char * app_info, int info_size )
{
	apdu * newApdu  =  NULL;
	newApdu = get_apdu_mgr()->create_apdu_update_application(app_id, app_info, info_size);

	mk_auto_ptr<apdu> ap(newApdu);

	if( 0 != get_dev_mgr()->transmit_apdu(devHandle, newApdu, &g_sw))
	{
		return 1;
	}

	if(g_sw != 0x9000)
	{
		return 2;
	}

	return 0;
}

int app_enum_applications( HANDLE devHandle, char * app_names, int * buff_size )
{
	apdu * newApdu  =  NULL;
	newApdu = get_apdu_mgr()->create_apdu_enum_application();

	mk_auto_ptr<apdu> ap(newApdu);

	
	if( 0 != get_dev_mgr()->transmit_apdu(devHandle,newApdu, &g_sw))
	{
		return 1;
	}

	if(g_sw != 0x9000 && g_sw != 0x6A9E)
	{
		return 2;
	}

	int data_len =0;
	unsigned char * pData = newApdu->get_response_data(&data_len);

	if(*buff_size < data_len)
	{
		return 3;
	}

	*buff_size = data_len;
	memcpy(app_names, pData, data_len);

	return 0;
}

int app_delete_application( HANDLE devHandle, char * app_name )
{
	apdu * newApdu  =  NULL;
	newApdu = get_apdu_mgr()->create_apdu_delete_application(app_name);

	mk_auto_ptr<apdu> ap(newApdu);

	
	if( 0 != get_dev_mgr()->transmit_apdu(devHandle,newApdu, &g_sw))
	{
		return 1;
	}

	if(g_sw != 0x9000)
	{
		return 2;
	}

	return 0;
}

int app_open_application( HANDLE devHandle, char * app_name, 
						 unsigned char * app_props, int props_size )
{
	apdu * newApdu  =  NULL;
	newApdu = get_apdu_mgr()->create_apdu_open_application(app_name);

	mk_auto_ptr<apdu> ap(newApdu);

	
	if( 0 != get_dev_mgr()->transmit_apdu(devHandle,newApdu, &g_sw))
	{
		return 1;
	}

	if(g_sw != 0x9000)
	{
		return 2;
	}

	int data_len = 0;
	unsigned char * props = newApdu->get_response_data(&data_len);

	if(props_size < data_len)
	{
		return 3;
	}

	memcpy(app_props, props, data_len);

	return 0;
}

int app_close_application( HANDLE devHandle, int app_id )
{
	apdu * newApdu  =  NULL;
	newApdu = get_apdu_mgr()->create_apdu_close_application(app_id);

	mk_auto_ptr<apdu> ap(newApdu);

	
	if( 0 != get_dev_mgr()->transmit_apdu(devHandle,newApdu, &g_sw))
	{
		return 1;
	}

	if(g_sw != 0x9000)
	{
		return 2;
	}

	return 0;
}

int app_get_application_ext_info(HANDLE devHandle, int app_id, unsigned char * ext_info, int * info_size)
{
	apdu * newApdu  =  NULL;
	newApdu = get_apdu_mgr()->create_apdu_get_ext_app_info(app_id);

	mk_auto_ptr<apdu> ap(newApdu);

	if( 0 != get_dev_mgr()->transmit_apdu(devHandle,newApdu, &g_sw))
	{
		return 1;
	}

	if(g_sw != 0x9000)
	{
		return 2;
	}

	int data_len = 0;
	unsigned char * props = newApdu->get_response_data(&data_len);

	if(*info_size < data_len)
	{
		return 3;
	}

	memcpy(ext_info, props, data_len);

	return 0;
}
